Skip to main content

Hooks

useEntityListQuery

Use this hook to fetch a list of entries. For example load a list of properties.

import { useEntityListQuery } from '@rexlabs/model-generator';

const propertiesQuery = ... // list of properties query

function Component() {
const { data, errors, status, actions } = useEntityListQuery(propertiesQuery, options);

...
}

Parameters:

NameTypeDescription
queryQuery | nullThe query to fetch
options{fetch?: boolean}?

Returns: Object

NameTypeDescription
dataany[]The data the query has fetched
errorsAPIError[]Array of errors that occurred when fetching the query
statusloading | error | loaded
actionsEntityActionsAny default actions for the model, plus all the custom actions defined
paginationPaginationpagination is only returned when status is loaded

useEntityQuery

Use this hook to fetch a query of a single entity. For example load a single property.

import { useEntityQuery } from '@rexlabs/model-generator';

const propertyQuery = ... // single property query

function Component() {
const { data, errors, status, actions } = useEntityQuery(propertyQuery, options);

...
}

Parameters:

NameTypeDescription
queryQueryThe query to fetch
options{fetch?: boolean, throwOnError?: boolean}?

Returns: Object

NameTypeDescription
dataanyThe data the query has fetched
errorsAPIError[]Array of errors that occurred when fetching the query
statusloading | error | loaded
actionsEntityActionsAny default actions for the model, plus all the custom actions defined

useModelActions

This hook lets you access all actions (default and custom) associated with a given model. An entity model would include the default CRUD actions.

import { useModelActions } from '@rexlabs/model-generator';

function Component() {
const { deleteItem, ...allOtherActions } = useModelActions(propertiesModel);

...
}

Parameters:

NameTypeDescription
modelModel

Returns: Object

NameTypeDescription
deleteItem({id: string}) => Promise\<unknown>Delete the entry with the corresponding id
trashItem({id: string}) => Promise\<unknown>Delete the entry with the corresponding id
updateItem({id: string, data: any}) => Promise\<Entry>Update a specific entry
createItem({data: any}) => Promise\<Entry>Creates a new entry
fetchItem({id: string}) => Promise\<Entry>Fetches a specific entry
fetchList() => Promise\<unknown>Fetches all entries of a model
fetchList() => Promise\<unknown>Fetches all entries of a model

useModelState

Access the current state of a given model.

import { useModelState } from '@rexlabs/model-generator';
import { currentUserModel } from 'models/current-user'

function Component() {
const {name, email} = useModelState(currentUserModel);

...
}

Parameters:

NameTypeDescription
modelModel

Returns: ModelDataObject